package com.youkeyi.ddy.cloud.application.system.user.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.youkeyi.ddy.cloud.common.api.application.system.position.dto.SystemPositionListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.system.user.SystemUserCloudService;
import com.youkeyi.ddy.cloud.common.api.application.system.user.dto.*;
import com.youkeyi.ddy.cloud.common.api.application.system.user.param.*;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.employee.HrEmployeeCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.employee.dto.HrEmployeeListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.employee.param.HrEmployeeQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.PublicCorpCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.dto.PublicCorpListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.dto.PublicCorpListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.param.PublicCorpQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.mapping.PublicMappingCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.mapping.dto.PublicMappingListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.mapping.dto.PublicMappingListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.mapping.param.PublicMappingQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.organization.PublicOrganizationCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.organization.dto.PublicOrganizationListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.organization.dto.PublicOrganizationListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.organization.param.PublicOrganizationQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.position.PublicPositionCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.position.dto.PublicPositionListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.position.dto.PublicPositionListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.position.param.PublicPositionQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.user.PublicUserAuthLoginCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.user.dto.PublicUserAuthLoginListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.user.param.PublicUserAuthLoginQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.user.PublicUserCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.user.dto.PublicUserListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.user.dto.PublicUserListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.user.param.*;
import com.youkeyi.ddy.cloud.common.config.TestCorpConfig;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyApplicationSystemConstant;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.context.YoukeyiSystemContext;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import com.youkeyi.ddy.cloud.common.utils.RedisUtils;
import com.youkeyi.ddy.cloud.common.utils.SecurityUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * SystemUserController
 *
 * @Author liuxiawang
 * @Date 2024/2/29 11:54 AM
 * @Location shenzhen.china
 */
@Service
public class SystemUserController implements SystemUserCloudService {

    @Autowired
    private PublicUserCloudService publicUserCloudService;
    @Autowired
    private PublicOrganizationCloudService publicOrganizationCloudService;
    @Autowired
    private PublicPositionCloudService publicPositionCloudService;
    @Autowired
    private PublicMappingCloudService publicMappingCloudService;
    @Autowired
    private PublicCorpCloudService publicCorpCloudService;
    @Autowired
    private PublicUserAuthLoginCloudService publicUserAuthLoginCloudService;
    @Autowired
    private HrEmployeeCloudService hrEmployeeCloudService;

    @Override
    public SystemUserListCloudDto list(SystemUserListCloudParam param) {
        SystemUserListCloudDto dto = new SystemUserListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        Integer isRoot = YoukeyiSystemContext.getIsRoot();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        PublicUserQueryCloudParam userQueryCloudParam = new PublicUserQueryCloudParam();
        userQueryCloudParam.setUserPrimaryKeys(param.getUserPrimaryKeys());
        if (2 != isRoot) {
            userQueryCloudParam.setCorpKeys(Collections.singletonList(corpKey));
        }
        userQueryCloudParam.setNickNameFuzzy(param.getNickNameFuzzy());
        userQueryCloudParam.setPhoneFuzzy(param.getPhoneFuzzy());
        userQueryCloudParam.setEmailFuzzy(param.getEmailFuzzy());
        userQueryCloudParam.setAccountFuzzy(param.getAccountFuzzy());
        userQueryCloudParam.setNameFuzzy(param.getNameFuzzy());
        userQueryCloudParam.setCodes(param.getCodes());
        userQueryCloudParam.setPageSize(param.getPageSize());
        userQueryCloudParam.setCurrentPage(param.getCurrentPage());
        userQueryCloudParam.setNeedPosition(Boolean.TRUE);
        PublicUserListCloudDto userDtos = publicUserCloudService.query(userQueryCloudParam);
        if (null != userDtos && CollectionUtils.isNotEmpty(userDtos.getRows())) {
            // 查询主体
            PublicCorpQueryCloudParam corpQueryCloudParam = new PublicCorpQueryCloudParam();
            corpQueryCloudParam.setCorpPrimaryKeys(userDtos.getRows().stream().map(PublicUserListItemCloudDto::getCorpKey).collect(Collectors.toList()));
            PublicCorpListCloudDto corpDtos = publicCorpCloudService.query(corpQueryCloudParam);
            Map<String, String> corpMap = new HashMap<>();
            if (null != corpDtos && CollectionUtils.isNotEmpty(corpDtos.getRows())) {
                corpMap.putAll(corpDtos.getRows().stream().collect(Collectors.toMap(PublicCorpListItemCloudDto::getCorpPrimaryKey, PublicCorpListItemCloudDto::getName, (k1, k2) -> k1)));
            }

            List<SystemUserListItemCloudDto> items = new ArrayList<>();
            userDtos.getRows().forEach(user -> {
                SystemUserListItemCloudDto item = new SystemUserListItemCloudDto();
                item.setCorpPrimaryKey(user.getCorpKey());
                item.setCorpName(corpMap.get(user.getCorpKey()));
                item.setUserPrimaryKey(user.getUserPrimaryKey());
                item.setCode(user.getCode());
                item.setName(user.getName());
                item.setNickName(user.getNickName());
                item.setPhone(user.getPhone());
                item.setAccount(user.getAccount());
                item.setIsRoot(user.getIsRoot());
                item.setEmail(user.getEmail());
                item.setAvatarUrl(user.getAvatarUrl());
                item.setIsDeleted(user.getIsDeleted());
                item.setIsDeletedName(user.getIsDeletedName());
                item.setCreateUserKey(user.getCreateUserKey());
                item.setCreateTime(user.getCreateTime());
                item.setCreateUserName(user.getCreateUserName());

                // 岗位信息
                if (CollectionUtils.isNotEmpty(user.getPositions())) {
                    user.getPositions().forEach(position -> {
                        SystemPositionListItemCloudDto positionItem = new SystemPositionListItemCloudDto();
                        positionItem.setPositionPrimaryKey(position.getPositionPrimaryKey());
                        positionItem.setName(position.getName());
                        positionItem.setCode(position.getCode());
                        positionItem.setDesc(position.getDesc());
                        item.getPositions().add(positionItem);
                    });
                }
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(userDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public SystemUserListByOrgCloudDto listByOrg(SystemUserListByOrgCloudParam param) {
        SystemUserListByOrgCloudDto dto = new SystemUserListByOrgCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        Map<String, PublicOrganizationListItemCloudDto> organizationMap = new HashMap<>();
        Map<String, PublicPositionListItemCloudDto> positionMap = new HashMap<>();
        Map<String, String> mappingMap = new HashMap<>();

        PublicOrganizationQueryCloudParam organizationQueryCloudParam = new PublicOrganizationQueryCloudParam();
        organizationQueryCloudParam.setOrganizationPrimaryKeys(param.getOrganizationPrimaryKeys());
        // 非测试主体就查询的时候剔除测试主体
        if (!TestCorpConfig.TestCorpKey.contains(corpKey)) {
            organizationQueryCloudParam.setNotInOrganizationPrimaryKeys(Arrays.asList(TestCorpConfig.TestCorpKey.split("\\|")));
        }
        PublicOrganizationListCloudDto organizations = publicOrganizationCloudService.query(organizationQueryCloudParam);
        if (null != organizations && CollectionUtils.isNotEmpty(organizations.getRows())) {
            organizationMap.putAll(organizations.getRows().stream().collect(Collectors.toMap(PublicOrganizationListItemCloudDto::getOrganizationPrimaryKey, Function.identity(), (k1, k2) -> k1)));

            // 岗位信息
            PublicPositionQueryCloudParam positionQueryCloudParam = new PublicPositionQueryCloudParam();
            positionQueryCloudParam.setOrganizationKeys(organizations.getRows().stream().map(PublicOrganizationListItemCloudDto::getOrganizationPrimaryKey).collect(Collectors.toList()));
            PublicPositionListCloudDto positionDtos = publicPositionCloudService.query(positionQueryCloudParam);
            if (null != positionDtos && CollectionUtils.isNotEmpty(positionDtos.getRows())) {
                positionMap.putAll(positionDtos.getRows().stream().collect(Collectors.toMap(PublicPositionListItemCloudDto::getPositionPrimaryKey, Function.identity(), (k1, k2) -> k1)));

                // 查询用户
                PublicMappingQueryCloudParam publicMappingQueryCloudParam = new PublicMappingQueryCloudParam();
                publicMappingQueryCloudParam.setMappingType(YoukeyiDdyCloudBasicPublicConstant.PUBLIC_MAPPING_TYPE_USER_POSITION.value());
                publicMappingQueryCloudParam.setKeyType(YoukeyiDdyCloudBasicPublicConstant.PUBLIC_MAPPING_KEY_TYPE_TARGET.value());
                publicMappingQueryCloudParam.setKeys(positionDtos.getRows().stream().map(PublicPositionListItemCloudDto::getPositionPrimaryKey).collect(Collectors.toList()));
                PublicMappingListCloudDto publicMappingDtos = publicMappingCloudService.query(publicMappingQueryCloudParam);
                if (null != publicMappingDtos && CollectionUtils.isNotEmpty(publicMappingDtos.getRows())) {
                    mappingMap.putAll(publicMappingDtos.getRows().stream().collect(Collectors.toMap(PublicMappingListItemCloudDto::getSourceKey, PublicMappingListItemCloudDto::getTargetKey, (k1, k2) -> k1)));

                    PublicUserQueryCloudParam userQueryCloudParam = new PublicUserQueryCloudParam();
                    userQueryCloudParam.setUserPrimaryKeys(publicMappingDtos.getRows().stream().map(PublicMappingListItemCloudDto::getSourceKey).collect(Collectors.toList()));
                    PublicUserListCloudDto userDtos = publicUserCloudService.query(userQueryCloudParam);
                    if (null != userDtos && CollectionUtils.isNotEmpty(userDtos.getRows())) {
                        List<SystemUserListByOrgItemCloudDto> items = new ArrayList<>();
                        userDtos.getRows().forEach(user -> {
                            SystemUserListByOrgItemCloudDto item = new SystemUserListByOrgItemCloudDto();
                            item.setUserPrimaryKey(user.getUserPrimaryKey());
                            item.setUserCode(user.getCode());
                            item.setUserName(user.getName());
                            item.setUserNickName(user.getNickName());
                            item.setUserPhone(user.getPhone());
                            item.setUserEmail(user.getEmail());
                            item.setUserAvatarUrl(user.getAvatarUrl());
                            if (mappingMap.containsKey(user.getUserPrimaryKey())) {
                                if (positionMap.containsKey(mappingMap.get(user.getUserPrimaryKey()))) {
                                    PublicPositionListItemCloudDto position = positionMap.get(mappingMap.get(user.getUserPrimaryKey()));
                                    item.setPositionPrimaryKey(position.getPositionPrimaryKey());
                                    item.setPositionName(position.getName());
                                    item.setPositionCode(position.getCode());

                                    if (organizationMap.containsKey(position.getOrganizationKey())) {
                                        PublicOrganizationListItemCloudDto organization = organizationMap.get(position.getOrganizationKey());
                                        item.setOrganizationPrimaryKey(organization.getOrganizationPrimaryKey());
                                        item.setOrganizationName(organization.getName());
                                        item.setOrganizationCode(organization.getCode());
                                    }
                                }
                            }
                            items.add(item);
                        });
                        dto.setRows(items);
                    }
                }
            }
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public SystemUserDetailCloudDto detail(String userPrimaryKey) {
        SystemUserDetailCloudDto dto = new SystemUserDetailCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (StringUtils.isBlank(userPrimaryKey)) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        PublicUserQueryCloudParam userQueryCloudParam = new PublicUserQueryCloudParam();
        // userQueryCloudParam.setCorpKeys(Collections.singletonList(corpKey));
        userQueryCloudParam.setUserPrimaryKeys(Collections.singletonList(userPrimaryKey));
        PublicUserListCloudDto users = publicUserCloudService.query(userQueryCloudParam);
        if (null != users && CollectionUtils.isNotEmpty(users.getRows())) {
            PublicUserListItemCloudDto user = users.getRows().get(0);
            dto.setUserPrimaryKey(user.getUserPrimaryKey());
            dto.setUserCode(user.getCode());
            dto.setName(user.getName());
            dto.setNickName(user.getNickName());
            dto.setPhone(user.getPhone());
            dto.setAccount(user.getAccount());
            dto.setIsRoot(user.getIsRoot());
            dto.setEmail(user.getEmail());
            dto.setAvatarUrl(user.getAvatarUrl());
            dto.setOpenId(user.getOpenId());
            dto.setUnionId(user.getUnionId());
            dto.setOfficialOpenId(user.getOfficialOpenId());
            dto.setRemark(user.getRemark());
            dto.setIsDeleted(user.getIsDeleted());
            dto.setIsDeletedName(user.getIsDeletedName());
            dto.setCreateUserKey(user.getCreateUserKey());
            dto.setCreateUserName(user.getCreateUserName());
            dto.setCreateTime(user.getCreateTime());
            dto.setUpdateUserKey(user.getUpdateUserKey());
            dto.setUpdateUserName(user.getUpdateUserName());
            dto.setUpdateTime(user.getUpdateTime());

            // 岗位信息
            if (CollectionUtils.isNotEmpty(user.getPositions())) {
                // 岗位信息
                PublicPositionQueryCloudParam positionQueryCloudParam = new PublicPositionQueryCloudParam();
                positionQueryCloudParam.setPositionPrimaryKeys(Collections.singletonList(user.getPositions().get(0).getPositionPrimaryKey()));
                positionQueryCloudParam.setNeedOrganization(Boolean.TRUE);
                PublicPositionListCloudDto positionDtos = publicPositionCloudService.query(positionQueryCloudParam);
                if (null != positionDtos && CollectionUtils.isNotEmpty(positionDtos.getRows())) {
                    PublicPositionListItemCloudDto position = positionDtos.getRows().get(0);

                    SystemPositionListItemCloudDto positionItem = new SystemPositionListItemCloudDto();
                    positionItem.setPositionPrimaryKey(position.getPositionPrimaryKey());
                    positionItem.setName(position.getName());
                    positionItem.setCode(position.getCode());
                    positionItem.setDesc(position.getDesc());
                    positionItem.setOrganizationPrimaryKey(position.getOrganizationKey());
                    if (null != position.getOrganization()) {
                        positionItem.setOrganizationName(position.getOrganization().getName());
                    }
                    dto.getPositions().add(positionItem);
                }
            }
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(SystemUserCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getName())
                || StringUtils.isBlank(param.getCode())
                || StringUtils.isBlank(param.getPhone())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        String userKey = null;
        HrEmployeeQueryCloudParam employeeQueryCloudParam = new HrEmployeeQueryCloudParam();
        employeeQueryCloudParam.setPhones(Collections.singletonList(param.getPhone()));
        HrEmployeeListCloudDto employeeDtos = hrEmployeeCloudService.query(employeeQueryCloudParam);
        if (null != employeeDtos && CollectionUtils.isNotEmpty(employeeDtos.getRows())) {
            userKey = employeeDtos.getRows().get(0).getEmployeePrimaryKey();
        }

        PublicUserCreateCloudParam userCreateCloudParam = new PublicUserCreateCloudParam();
        userCreateCloudParam.setUserPrimaryKey(userKey);
        userCreateCloudParam.setCorpKey(StringUtils.defaultIfEmpty(param.getCorpKey(), corpKey));
        userCreateCloudParam.setName(param.getName());
        userCreateCloudParam.setCode(param.getCode());
        userCreateCloudParam.setNickName(StringUtils.defaultIfEmpty(param.getNickName(), param.getName()));
        userCreateCloudParam.setPhone(param.getPhone());
        userCreateCloudParam.setEmail(param.getEmail());
        userCreateCloudParam.setIsRoot(param.getIsRoot());
        userCreateCloudParam.setPassWord(param.getPassWord());
        userCreateCloudParam.setAvatarUrl(param.getAvatarUrl());
        userCreateCloudParam.setRemark(param.getRemark());
        userCreateCloudParam.setPositionKeys(param.getPositionKeys());
        userCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        userCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
        return publicUserCloudService.create(userCreateCloudParam);
    }

    @Override
    public ResponseContent updatePassword(HttpServletRequest request, HttpServletResponse response, SystemUserUpdatePasswordCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String token = request.getHeader(YoukeyiDdyApplicationSystemConstant.AUTH_REDIS_TOKEN_KEY);
        String tenantKey = YoukeyiSystemContext.getTenantPrimaryKey();
        String userPrimaryKey = YoukeyiSystemContext.getUserPrimaryKey();
        if (StringUtils.isBlank(tenantKey) || StringUtils.isBlank(token) || StringUtils.isBlank(userPrimaryKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param || StringUtils.isBlank(param.getBodyStr())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        Map<String, String> loginKeyMap;
        try {
            loginKeyMap = new HashMap<>(RedisUtils.getHashMapFromJedis(YoukeyiDdyApplicationSystemConstant.AUTH_REDIS_DB_INDEX, token));
        } catch (Exception e) {
            e.printStackTrace();
            rc.setErrorMessage("获取密钥失败");
            return rc;
        }
        if (!loginKeyMap.containsKey(YoukeyiDdyApplicationSystemConstant.AUTH_REDIS_PRIVATE_KEY) || StringUtils.isBlank(loginKeyMap.get(YoukeyiDdyApplicationSystemConstant.AUTH_REDIS_PRIVATE_KEY))) {
            rc.setErrorMessage("获取私钥失败");
            return rc;
        }

        String oldPassword;
        String newPassword;
        try {
            String decryptStr = SecurityUtils.rsaDecryptByPrivateKey(param.getBodyStr(), loginKeyMap.get(YoukeyiDdyApplicationSystemConstant.AUTH_REDIS_PRIVATE_KEY));
            JSONObject loginData = JSON.parseObject(decryptStr);
            oldPassword = loginData.getString("oldPassword");
            newPassword = loginData.getString("newPassword");
        } catch (Exception e) {
            e.printStackTrace();
            rc.setErrorMessage("解密失败:" + e.getMessage());
            return rc;
        }
        if (StringUtils.isBlank(oldPassword) || StringUtils.isBlank(newPassword)) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }


        PublicUserQueryCloudParam userQueryCloudParam = new PublicUserQueryCloudParam();
        userQueryCloudParam.setUserPrimaryKeys(Collections.singletonList(userPrimaryKey));
        PublicUserListCloudDto userDtos = publicUserCloudService.query(userQueryCloudParam);
        if (userDtos != null && CollectionUtils.isNotEmpty(userDtos.getRows())) {
            PublicUserListItemCloudDto user = userDtos.getRows().get(0);
            // 老密码加密，对比是够正确
            String encryptPassword = SecurityUtils.md5(oldPassword, user.getUserPrimaryKey());
            if (!user.getPassWord().equals(encryptPassword)) {
                rc.setErrorMessage("旧密码校验失败");
                return rc;
            }
            PublicUserUpdatePasswordCloudParam userUpdateCloudParam = new PublicUserUpdatePasswordCloudParam();
            userUpdateCloudParam.setUserPrimaryKey(userPrimaryKey);
            userUpdateCloudParam.setPasswordOld(oldPassword);
            userUpdateCloudParam.setPasswordNew(newPassword);
            return publicUserCloudService.updatePassword(userUpdateCloudParam);
        } else {
            rc.setErrorMessage("用户不存在无法修改密码");
        }
        return rc;
    }

    @Override
    public ResponseContent update(SystemUserUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getUserPrimaryKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        PublicUserUpdateCloudParam userUpdateCloudParam = new PublicUserUpdateCloudParam();
        userUpdateCloudParam.setUserPrimaryKey(param.getUserPrimaryKey());
        userUpdateCloudParam.setName(param.getName());
        userUpdateCloudParam.setCode(param.getCode());
        userUpdateCloudParam.setNickName(param.getNickName());
        userUpdateCloudParam.setPhone(param.getPhone());
        userUpdateCloudParam.setEmail(param.getEmail());
        userUpdateCloudParam.setIsRoot(param.getIsRoot());
        userUpdateCloudParam.setAvatarUrl(param.getAvatarUrl());
        userUpdateCloudParam.setOpenId(param.getOpenId());
        userUpdateCloudParam.setUnionId(param.getUnionId());
        userUpdateCloudParam.setOfficialOpenId(param.getOfficialOpenId());
        userUpdateCloudParam.setRemark(param.getRemark());
        userUpdateCloudParam.setPositionKeys(param.getPositionKeys());
        userUpdateCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        userUpdateCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        userUpdateCloudParam.setUpdatePosition(true);
        return publicUserCloudService.update(userUpdateCloudParam);
    }

    @Override
    public ResponseContent delete(SystemUserDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param || CollectionUtils.isEmpty(param.getUserPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        PublicUserDeleteCloudParam userDeleteCloudParam = new PublicUserDeleteCloudParam();
        userDeleteCloudParam.setUserPrimaryKeys(param.getUserPrimaryKeys());
        userDeleteCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        userDeleteCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return publicUserCloudService.delete(userDeleteCloudParam);
    }


    @Override
    public ResponseContent updateAvatarUrl(SystemUserUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        String userPrimaryKey = YoukeyiSystemContext.getUserPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }

        PublicUserUpdateCloudParam userUpdateCloudParam = new PublicUserUpdateCloudParam();
        userUpdateCloudParam.setUserPrimaryKey(userPrimaryKey);
        userUpdateCloudParam.setAvatarUrl(param.getAvatarUrl());
        userUpdateCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        userUpdateCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return publicUserCloudService.update(userUpdateCloudParam);

    }

    @Override
    public ResponseContent updateNickName(SystemUserUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String tenantKey = YoukeyiSystemContext.getTenantPrimaryKey();
        if (StringUtils.isBlank(tenantKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getUserPrimaryKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        PublicUserUpdateCloudParam userUpdateCloudParam = new PublicUserUpdateCloudParam();
        userUpdateCloudParam.setUserPrimaryKey(param.getUserPrimaryKey());
        userUpdateCloudParam.setCorpKey(tenantKey);

        userUpdateCloudParam.setNickName(param.getNickName());
        userUpdateCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        userUpdateCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return publicUserCloudService.updateNickName(userUpdateCloudParam);
    }

    @Override
    public SystemUserAuthLoginListCloudDto authList(SystemUserAuthLoginListCloudParam param) {
        SystemUserAuthLoginListCloudDto dto = new SystemUserAuthLoginListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        PublicUserAuthLoginQueryCloudParam userAuthLoginQueryCloudParam = new PublicUserAuthLoginQueryCloudParam();
        userAuthLoginQueryCloudParam.setPageSize(param.getPageSize());
        userAuthLoginQueryCloudParam.setCurrentPage(param.getCurrentPage());
        PublicUserAuthLoginListCloudDto userAuthLoginDtos = publicUserAuthLoginCloudService.query(userAuthLoginQueryCloudParam);
        if (null != userAuthLoginDtos && CollectionUtils.isNotEmpty(userAuthLoginDtos.getRows())) {
            List<SystemUserAuthLoginListItemCloudDto> items = new ArrayList<>();
            userAuthLoginDtos.getRows().forEach(authLogin -> {
                SystemUserAuthLoginListItemCloudDto item = new SystemUserAuthLoginListItemCloudDto();
                item.setUserAuthLoginPrimaryKey(authLogin.getUserAuthLoginPrimaryKey());
                item.setUserKey(authLogin.getUserKey());
                item.setUserName(authLogin.getUserName());
                item.setAuthCorpKey(authLogin.getAuthCorpKey());
                item.setAuthCorpCoordinate(authLogin.getAuthCorpCoordinate());
                item.setAuthAccount(authLogin.getAuthAccount());
                item.setAuthPhone(authLogin.getAuthPhone());
                item.setAuthTime(authLogin.getAuthTime());
                item.setRemark(authLogin.getRemark());
                item.setIsDeleted(authLogin.getIsDeleted());
                item.setIsDeletedName(authLogin.getIsDeletedName());
                item.setCreateUserKey(authLogin.getCreateUserKey());
                item.setCreateTime(authLogin.getCreateTime());
                item.setCreateUserName(authLogin.getCreateUserName());
                item.setUpdateUserKey(authLogin.getUpdateUserKey());
                item.setUpdateUserName(authLogin.getUpdateUserName());
                item.setUpdateTime(authLogin.getUpdateTime());
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(userAuthLoginDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }
}
